Component({
  mixins: [],
  data: {
    previewData: {},
    onlineData: {},
    activityData: {},
    previewUrl: '',
    onlineUrl: ''
  },
  props: {},
  didMount() {
    this.loadData();
    this.loadInstance()
  },
  didUpdate() { },
  didUnmount() { },
  methods: {

    copyPreviewCode() {
      my.setClipboard({
        text: this.data.previewData.pre_view_url
      });
      my.alert({
        content: '复制成功！'
      })
    },
    copyOnlineCode() {
      my.setClipboard({
        text: this.data.onlineData.online_url
      });
      my.alert({
        content: '复制成功！'
      })
    },
    async intanceCode() {
      const { cloud } = getApp();
      try {
        const res = await cloud.topApi.invoke({
          api: 'taobao.miniapp.template.instantiate',
          authScope: '*',
          data: {
            clients: 'taobao,tmall',
            description: '欢迎来到分享有礼活动',
            ext_json: `{'shop_id':${this.data.activityData.shop_id}}`,
            icon: this.data.activityData.shop_logo,
            name: this.data.activityData.name,
            template_id: '3000000002163563',
            template_version: '0.0.4'
          }
        });
        this.setData({
          previewData: res,
        })
        this.writeInstance()

      } catch (e) {
        my.alert({ content: 'error ' + e.message })
      }
    },
    async submitCode() {
      const { cloud } = getApp();
      try {
        const res = await cloud.topApi.invoke({
          api: 'taobao.miniapp.template.instantiate',
          authScope: '*',
          data: {
            clients: 'taobao,tmall',
            app_id: this.data.previewData.app_id,
            template_id: '3000000002163563',
            template_version: '0.0.4',
            app_version: this.data.previewData.app_version
          }
        });
        this.setData({
          onlineData: res.app_info,
        })

      } catch (e) {
        my.alert({ content: 'error ' + e.message })
      }
    }
  },
  async loadData() {
    const { cloud } = getApp();
    try {
      const res = await cloud.application.httpRequest({
        path: '/index.php/index/qn/getset',
        methods: 'GET',
        params: {
          shop_id: getApp().globalData.shop_id
        }
      });
      this.setData({
        activityData: res
      })
    } catch (e) {
      my.alert({ content: 'error ' + e.message })
    }
  },
  async loadInstance() {
    const { cloud } = getApp();
    const res = await cloud.application.httpRequest({
      path: '/index.php/index/qn/getinstance',
      method: 'GET',
      params: { shop_id: getApp().globalData.shop_id }
    })

    if (res) {
      this.setData({
        previewData: res
      })
    }
  },
  async writeInstance() {
    const { cloud } = getApp();
    let sendData = {
      shop_id: getApp().globalData.shop_id,
      appkey: this.data.previewData.appkey,
      app_name: this.data.previewData.app_name,
      app_description: this.data.previewData.app_description,
      app_icon: this.data.previewData.app_icon,
      app_id: this.data.previewData.app_id,
      pre_view_url: this.data.previewData.pre_view_url,
      app_version: this.data.previewData.app_version,
    };
    const res = await cloud.application.httpRequest({
      path: '/index.php/index/qn/writeinstance',
      method: 'POST',
      params: sendData
    })
  },
});
